package com.meihua.shellapp.fragment.policy;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.adapter.personalcenter.PolicyFragmentAdapter;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.PolicyBean;
import com.meihua.shellapp.databean.PolicyDataBean;
import com.meihua.shellapp.databean.SupplierDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/26.
 * 邮箱：lc0607live@163.com
 */

public class PolicyManageFragment extends BaseFragment {

    public int type;
    public static List<PolicyManageFragment> mList;//所有开启的orderfragment
    private PolicyFragmentAdapter mAdapter;

    private String url = "";
    private String webUrl = "";
    private List<PolicyDataBean> mData = new ArrayList<>();

    private String insured_date1;
    private String insured_date2;
    private String policy_status;
    private String insurance_type;
    private String policy_holder_name;
    private String provider_id;
    private int mPage = 1;
    private boolean isvisible;
    private LoadMoreFooterView loadMoreFooterView;


    /**
     * 设置type
     *
     * @param type 根据类型获取 1，全部   2，险种
     * @return
     */
    public PolicyManageFragment setType(int type) {
        this.type = type;
        if (type == 1) {
            webUrl = URL.getUrl().WEBGUARANTY;
            url = URL.getUrl().FIRSTPOLICY;
        } else if (type == 2) {
            url = URL.getUrl().RENEWALPOLICY;
            webUrl = URL.getUrl().WEBRENEWAL;
        } else {
            url = URL.getUrl().RECEIVABLE;
            webUrl = "www.baidu.com/";
        }
        return this;
    }

    @Bind(R.id.rv)
    IRecyclerView rv;//列表

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_layout_rv;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv.getLoadMoreFooterView();
        mAdapter = new PolicyFragmentAdapter(getActivity(), mData) {
            @Override
            public void click(PolicyDataBean data) {
                Intent in = new Intent(getContext(), WebActivity.class);
                in.putExtra("url", webUrl + data.getProduct_id());
                startActivity(in);
            }
        };
        rv.setOnLoadMoreListener(this);
        rv.setOnRefreshListener(this);
        rv.setIAdapter(mAdapter);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            getData();
            isvisible = true;
        } else {
            isvisible = false;
        }
    }

    /**
     * 设置筛选条件
     *
     * @param startTimeStr
     * @param endTimeStr
     * @param nameStr
     * @param typeIndex
     * @param statusIndex
     * @param supplierBean
     */
    public void setScreenCondition(String startTimeStr, String endTimeStr, String nameStr, int typeIndex, int statusIndex, SupplierDataBean supplierBean) {
        insured_date1 = startTimeStr.equals("开始日期") ? null : startTimeStr;
        insured_date2 = endTimeStr.equals("结束日期") ? null : endTimeStr;
        policy_status = statusIndex == 0 ? null : statusIndex + "";
        insurance_type = typeIndex == 0 ? null : typeIndex + "";
        policy_holder_name = nameStr.equals("") ? null : nameStr;
        provider_id = supplierBean == null ? null : supplierBean.getSup_id();
        mPage = 1;
        if (isvisible) {
            getData();
        }
    }

    /**
     * 获取数据
     */
    public void getData() {
        OkGo.<String>post(url)
                .params("salesman_number", MyApplication.getInstance().getUserId())
                .params("insured_date1", insured_date1)
                .params("insured_date2", insured_date2)
                .params("policy_status", policy_status)
                .params("insurance_type", insurance_type)
                .params("policy_holder_name", policy_holder_name)
                .params("provider_id", provider_id)
                .params("page", mPage)
                .execute(new MyStringCallBack<PolicyBean>(rv) {

                    @Override
                    public void onSuccessData(PolicyBean bean) {
                        super.onSuccessData(bean);
                        try{
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (mPage == 1) {
                                mData.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }}catch (Exception e){}
                    }

                });
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getData();
    }
}
